//
//  LoginViewController.m
//  ReactiveCocoa_Practice
//
//  Created by thinker on 15/8/28.
//  Copyright (c) 2015年 RayTao. All rights reserved.
//

#import "LoginViewController.h"

typedef void (^RWSignInResponse)(BOOL);

@interface LoginViewController ()

@property (weak, nonatomic) IBOutlet UITextField *usernameTextField;
@property (weak, nonatomic) IBOutlet UILabel *desTitle;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
@property (weak, nonatomic) IBOutlet UIButton *signinButton;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [[[self.usernameTextField.rac_textSignal map:^id(NSString *text) {
        return @(text.length);
    }] filter:^BOOL(NSNumber *value) {
        return [value integerValue] > 3;
    }] subscribeNext:^(NSNumber *x) {
        self.desTitle.text = [NSString stringWithFormat:@"用户名:%@个字节",x];
    }];

  //检测用户和密码是否可用，不可用就高亮表示
  RACSignal *validUsernameSignal = [[self.usernameTextField.rac_textSignal map:^id(NSString *text) {
    return @([self isValidUsername:text]);
  }] distinctUntilChanged];
  RACSignal *validPassword = [[self.passwordTextField.rac_textSignal map:^id(NSString *text) {
    return @([self isValidPassword:text]);
  }] distinctUntilChanged];
  RAC(self.usernameTextField,backgroundColor) = [validUsernameSignal map:^id(NSNumber *usernameValid) {
    NSLog(@"改变状态");
    return [usernameValid boolValue] ? [UIColor clearColor] : [UIColor yellowColor];
  }];
  RAC(self.passwordTextField,backgroundColor) = [validPassword map:^id(NSNumber *passwordValid) {
    return [passwordValid boolValue] ? [UIColor clearColor] : [UIColor yellowColor];
  }];
  
  //处理sign in按钮响应
  RACSignal *signUpActiveSignal = [RACSignal combineLatest:@[validPassword,validUsernameSignal]
                                   reduce:^id(NSNumber *passwordValid,NSNumber *usernameValid){
                                     return @([usernameValid boolValue] && [passwordValid boolValue]);
                                   }];
  [signUpActiveSignal subscribeNext:^(NSNumber *signupActive) {
    self.signinButton.enabled = [signupActive boolValue];
  }];
  [[[self.signinButton rac_signalForControlEvents:UIControlEventTouchUpInside] flattenMap:^RACStream *(id value) {
    return [self signInSignal];
  }] subscribeNext:^(id x) {
    NSLog(@"Sign in result: %@",x);
  }];
}

- (RACSignal *)signInSignal {
  return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
    [self signInWithUsername:self.usernameTextField.text password:self.passwordTextField.text complete:^(BOOL success) {
      [subscriber sendNext:@(success)];
      [subscriber sendCompleted];
      
    }];
    return nil;
  }];

}

- (void)signInWithUsername:(NSString *)username password:(NSString *)password complete:(RWSignInResponse)completeBlock {
  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    completeBlock(YES);
    NSLog(@"登陆成功");
  });
}

- (BOOL)isValidUsername:(NSString *)text {
  if (text.length > 3) {
    return YES;
  } else {
    return NO;
  }
}

- (BOOL)isValidPassword:(NSString *)text {
  if (text.length > 5) {
    return YES;
  } else {
    return NO;
  }
}

@end
